package com.kaily.timemanager.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.common.util.Logger;
import com.kaily.timemanager.R;
import com.kaily.timemanager.obj.TaskClass;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoulikai on 17/3/3.
 */

public class TaskClassLayout extends LinearLayout implements View.OnClickListener {

    private final String TAG = "TaskClassLayout";
    private CustomerListView taskClassListLayout;
    private TaskClassAdapter taskClassAdapter;
    private List<TaskClass> taskClasses = new ArrayList<>();
    private Button taskclassAddBtn;

    private ITaskClassActionListener listener;

    public TaskClassLayout(Context context) {
        super(context);
    }

    public TaskClassLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TaskClassLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TaskClassLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        taskClassListLayout = (CustomerListView) this.findViewById(R.id.taskclass_list_layout);
        taskclassAddBtn = (Button) this.findViewById(R.id.taskclass_add_btn);

        taskclassAddBtn.setOnClickListener(this);


    }

    public void initTaskClasses(List<TaskClass> taskClasses, ITaskClassActionListener listener) {
        this.taskClasses = taskClasses;
        taskClassAdapter = new TaskClassAdapter(this.getContext(), taskClasses);
//        if (taskClassAdapter != null) {
//            taskClassAdapter.notifyDataSetChanged();
//        }
        taskClassListLayout.setAdapter(taskClassAdapter);
//        taskClassListLayout.forceLayout();
        taskClassListLayout.notificationView();
        this.listener = listener;

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.taskclass_add_btn:
                addTaskClassAction();
                break;
        }

    }

    private void addTaskClassAction() {
        TaskClass taskClass = new TaskClass();
        taskClass.setTaskClassName("工作");
        if (this.listener != null) {
            this.listener.addTaskClassAction(taskClass, taskClassAdapter);
        }
    }

    public class TaskClassAdapter extends BaseAdapter {
        private List<TaskClass> taskClasses;
        private Context context;
        private LayoutInflater mInflater;

        private TaskClassAdapter(Context context, List<TaskClass> taskClasses) {
            this.context = context;
            this.taskClasses = taskClasses;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
            if (taskClassListLayout != null) {
                taskClassListLayout.notificationView();
            }
        }

        @Override
        public int getCount() {
            if (taskClasses == null) {
                return 0;
            }

            Logger.d(TAG, ">>>>getCount>>>>" + taskClasses);
            return taskClasses.size();
        }

        @Override
        public Object getItem(int i) {
            if (taskClasses == null) {
                return null;
            }
            return taskClasses.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.taskclass_item_layout, null);
                holder = new ViewHolder();
                /*得到各个控件的对象*/
                holder.tv = (TextView) convertView.findViewById(R.id.task_class_name_tv);

                convertView.setTag(holder); //绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag(); //取出ViewHolder对象
            }
            TaskClass taskClass = taskClasses.get(i);

            if (taskClass != null) {
                holder.tv.setText(taskClass.getTaskClassName());
            }

            return convertView;
        }

        /**
         * 存放控件
         */
        public class ViewHolder {

            public TextView tv;
        }

    }


}
